# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from scrapy_redis.spiders import  RedisCrawlSpider
from Books.items import YmxItem

class YmxSpider(RedisCrawlSpider):
    name = 'ymx'
    allowed_domains = ['amazon.cn']
    # start_urls = ['https://www.amazon.cn/b/?&node=116169071&tag=baiduiclickcn-23&ref=DEP_190527_lhb_06']
    redis_key =  "ymx:start_url"
    rules = (
        # 匹配大分类的url地址和小分类的url
        Rule(LinkExtractor(restrict_xpaths=('//ul[@class="a-unordered-list a-nostyle a-vertical s-ref-indent-two"]//li')), follow=True),
        # 匹配图书的url地址
        Rule(LinkExtractor(restrict_xpaths=('//div[@id="mainResults"]/ul/li/h2/..')), callback='parse_book_detail', follow=False),
        # 列表页翻页
        Rule(LinkExtractor(restrict_xpaths=('//div[@id="pagnNextLink"]')), follow=True),
    )

    def parse_book_detail(self, response):
        item = YmxItem()
        item['book_title'] = response.xpath('//span[@id="ebooksProductTitle"]/text()').getall()
        item['book_author'] = response.xpath('//span[@class="author notFaded"]//text()').getall()
        item['book_img'] = response.xpath('//div[@id="ebooks-img-canvas"]/img/@src').get()
        item['book_price'] = response.xpath('//span[@class="a-size-medium a-color-price"]/text()').get()
        item['book_heat'] = response.xpath('//span[@id="acrCustomerReviewText"]/text()').get()
        item['book_cate'] = response.xpath('//ul[@class="a-unordered-list a-horizontal a-size-small"]/li[5]/span/text').get()
        item['book_press'] = response.xpath('//b[text()="出版社:"]/../text()').get()
        item['book_evaluate'] = response.xpath('//div[@data-hook="review-collapsed"]/span/text()').getall()
        return item
